package com.ymttest.business.service;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.coupon.parameter.BuyerInfo;
import com.ymatou.iapi.coupon.parameter.OrderInfo;
import com.ymatou.iapi.coupon.parameter.ProductInfo;
import com.ymatou.iapi.coupon.parameter.SellerOrderInfo;
import com.ymatou.iapi.coupon.parameter.ShoppingInfo;
import com.ymatou.iapi.coupon.parameter.UseCouponBean;
import com.ymatou.iapi.coupon.parameter.UserReceiveCouponBean;
import com.ymatou.iapi.coupon.parameter.UserReceivePackageBean;
import com.ymatou.iapi.coupon.parameter.updateReceivedAndUsedCountBean;
import com.ymatou.iapi.coupon.parameter.userReceiveCouponBagBean;
import com.ymatou.iapi.coupon.service.UseCouponCall;
import com.ymatou.iapi.coupon.service.UserReceiveCouponCall;
import com.ymatou.iapi.coupon.service.updateReceivedAndUsedCountCall;
import com.ymatou.iapi.coupon.service.userReceiveCouponBagCall;
import com.ymatou.iapi.couponmanage.parameter.BatchInfo;
import com.ymatou.iapi.couponmanage.parameter.CouponValue;
import com.ymatou.iapi.couponmanage.parameter.CreateBagBean;
import com.ymatou.iapi.couponmanage.parameter.CreateBean;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.parameter.CreateSendTaskBean;
import com.ymatou.iapi.couponmanage.parameter.InvalidBatchBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.parameter.SendCouponBean;
import com.ymatou.iapi.couponmanage.parameter.SendSmallCouponBean;
import com.ymatou.iapi.couponmanage.parameter.TaskSendReqInfo;
import com.ymatou.iapi.couponmanage.parameter.UpdateApplyProductsBean;
import com.ymatou.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.ymatou.iapi.couponmanage.service.CreateBagCall;
import com.ymatou.iapi.couponmanage.service.CreateCall;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.service.CreateSendTaskCall;
import com.ymatou.iapi.couponmanage.service.InvalidBatchCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymatou.iapi.couponmanage.service.SendCouponCall;
import com.ymatou.iapi.couponmanage.service.UpdateApplyProductsCall;
import com.ymatou.iapi.couponmanage.service.createTaskByUserIdCall;
import com.ymatou.iapi.fileupload.service.UploadCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponupdatesetting;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponupdatesettingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
//http://wiki.ymatou.cn:8090/pages/viewpage.action?pageId=26148892
public class CouponServiceMCallServiceV2 {

	// 兑换码
	public CreateBean setDefaultCustomCreateBean(List<String> batchCodes) {
		CreateBean bean = new CreateBean();
		String num = TestDataManager.randomStr(8);

		bean.setBillName("自动化测试创建自定义兑换码单据_" + num);
		bean.setBillDes("描述_" + num);
		bean.setGenerateMethod(1);
		bean.setGenerateQuantity(1);
		bean.setCustomConversionCode("abcdefg_" + num);
		bean.setCreateUser("2222");
		bean.setCouponBatchCodes(batchCodes);

		bean.setBatchType(1);
		return bean;
	}

	public CreateBean setDefaultRandomCreateBean(List<String> batchCodes) {
		CreateBean bean = new CreateBean();
		String num = TestDataManager.randomStr(8);

		bean.setBillName("自动化测试创建随机兑换码单据_" + num);
		bean.setBillDes("描述_" + num);
		bean.setGenerateMethod(2);
		bean.setGenerateQuantity(2);
		bean.setCreateUser("2222");
		bean.setCouponBatchCodes(batchCodes);
		bean.setCustomConversionCode(num);
		bean.setBatchType(1);
		return bean;
	}

	public CreateCall create(CreateBean bean) throws ClientProtocolException,
			IOException {
		CreateCall call = new CreateCall();
		call.setData(bean);
		call.callService();

		return call;
	}
	// 平台批次
	public CreateCouponBean setDefaultPlatformCreateCouponBeanForRecieve() {
		CreateCouponBean createcouponBean = new CreateCouponBean();

		createcouponBean.setCouponName("自动化创建平台领取优惠券_"
				+ TestDataManager.getRandomNum(5));
		createcouponBean.setApplyMemo("自动化测试创建平台领取优惠券");
		createcouponBean.setSendType(2);
		createcouponBean.setCouponDes("平台领取优惠券描述-自动化");

		createcouponBean.setEffectiveType(0);
		createcouponBean.setEffectiveValidType(0);
		createcouponBean.setEffectiveDays(0);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		createcouponBean.setValidStart(dBefore);
		createcouponBean.setValidEnd(dAfter);

		createcouponBean.setCouponUseType(1);

		CouponValue couponValue = new CouponValue();
		couponValue.setOrderMinAmount(new BigDecimal(200));
		couponValue.setDeductionMaxAmount(new BigDecimal(1));
	
		List<CouponValue> couponValues = new ArrayList<CouponValue>();
		couponValues.add(couponValue);

		createcouponBean.setCouponValues(couponValues);
		createcouponBean.setCouponNum(2);
		createcouponBean.setPerCouponMaxUseCount(1);
		createcouponBean.setPerUserReceiveMaxCount(1);

		createcouponBean.setIsCustomCouponCode(false);
		createcouponBean.setCustomCouponCode("");

		createcouponBean.setUserType(0);

		List<Integer> usePlatforms = new ArrayList<Integer>();
		/*
		 * usePlatforms.add(1); usePlatforms.add(2); usePlatforms.add(3);
		 */

		createcouponBean.setUsePlatforms(usePlatforms);

		List<String> productIds = new ArrayList<String>();
		createcouponBean.setProductIds(productIds);

		List<Integer> sellerIds = new ArrayList<Integer>();
		createcouponBean.setSellerIds(sellerIds);

		List<Integer> activityIds = new ArrayList<Integer>();
		createcouponBean.setActivityIds(activityIds);

		List<Integer> brands = new ArrayList<Integer>();
		createcouponBean.setBrands(brands);

		List<Integer> categories = new ArrayList<Integer>();
		createcouponBean.setCategories(categories);

		List<Integer> Countrys = new ArrayList<Integer>();
		createcouponBean.setCountrys(Countrys);

		createcouponBean.setApplyUser("自动化user111");
		createcouponBean.setDepartment(1);
		String impresetInvoiceId = EnvSetup.getData("ImpresetInvoiceId");
		createcouponBean.setImpresetInvoiceId(impresetInvoiceId);
		createcouponBean.setOperatorId(222222);

		createcouponBean.setAcquireStartTime(dBefore);
		//新增
		createcouponBean.setAcquireEndTime(dAfter);
		// 获取优惠券方式(1:领取(默认) 2:兑换)
		createcouponBean.setAcquireCouponMethod(1);
		return createcouponBean;
	}

	
	// 平台批次
	public CreateCouponBean setDefaultPlatformCreateCouponBeanForConversion()
			throws ClientProtocolException, IOException {
		CreateCouponBean createcouponBean = new CreateCouponBean();

		createcouponBean.setCouponName("自动化创建平台兑换优惠券_"
				+ TestDataManager.getRandomNum(5));
		createcouponBean.setApplyMemo("自动化测试创建平台兑换优惠卷");
		createcouponBean.setSendType(2);
		createcouponBean.setCouponDes("平台兑换优惠卷描述-自动化");

		createcouponBean.setEffectiveType(0);
		createcouponBean.setEffectiveValidType(0);
		createcouponBean.setEffectiveDays(0);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		createcouponBean.setValidStart(dBefore);
		createcouponBean.setValidEnd(dAfter);

		createcouponBean.setCouponUseType(1);

		CouponValue couponValue = new CouponValue();
		couponValue.setOrderMinAmount(new BigDecimal(200));
		couponValue.setDeductionMaxAmount(new BigDecimal(10));

		List<CouponValue> couponValues = new ArrayList<CouponValue>();
		couponValues.add(couponValue);

		createcouponBean.setCouponValues(couponValues);
		createcouponBean.setCouponNum(2);
		createcouponBean.setPerCouponMaxUseCount(1);
		createcouponBean.setPerUserReceiveMaxCount(1);

		createcouponBean.setIsCustomCouponCode(false);
		createcouponBean.setCustomCouponCode("");

		createcouponBean.setUserType(0);

		List<Integer> usePlatforms = new ArrayList<Integer>();
		/*
		 * usePlatforms.add(1); usePlatforms.add(2); usePlatforms.add(3);
		 */

		createcouponBean.setUsePlatforms(usePlatforms);

		List<String> productIds = new ArrayList<String>();
		createcouponBean.setProductIds(productIds);

		List<Integer> sellerIds = new ArrayList<Integer>();
		createcouponBean.setSellerIds(sellerIds);

		List<Integer> activityIds = new ArrayList<Integer>();
		createcouponBean.setActivityIds(activityIds);

		List<Integer> brands = new ArrayList<Integer>();
		createcouponBean.setBrands(brands);

		List<Integer> categories = new ArrayList<Integer>();
		createcouponBean.setCategories(categories);
		List<Integer> countrys = new ArrayList<Integer>();
		createcouponBean.setCountrys(countrys);

		createcouponBean.setApplyUser("自动化user111");
		createcouponBean.setDepartment(1);
		String impresetInvoiceId = EnvSetup.getData("ImpresetInvoiceId");
		createcouponBean.setImpresetInvoiceId(impresetInvoiceId);
		createcouponBean.setOperatorId(222222);

		Calendar calendarstart = Calendar.getInstance(); // 得到日历
		calendarstart.setTime(dNow);// 把当前时间赋给日历
		calendarstart.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		Date dStart = calendarstart.getTime(); // 得到前一天的时间

		Calendar calendarend = Calendar.getInstance(); // 得到日历
		calendarend.setTime(dNow);// 把当前时间赋给日历
		calendarend.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		Date dEnd = calendarend.getTime(); // 得到前一天的时间

		createcouponBean.setAcquireStartTime(dStart);
		createcouponBean.setAcquireEndTime(dEnd);

		// 获取优惠券方式(1:领取(默认) 2:兑换)
		createcouponBean.setAcquireCouponMethod(2);

		return createcouponBean;
	}

	// 套券
	public CreateBagBean setDefaultPlatformCreateCouponBagBeanForRecieve()
			throws ClientProtocolException, IOException {
		CreateBagBean CreateBagBean = new CreateBagBean();
		CreateBagBean.setName("自动化创建套券领取优惠券");
		CreateBagBean.setDescription("自动化测试创建套券领取优惠券描述");
		CreateBagBean.setOperatorId(222222);
		CreateBagBean.setApplyUser("自动化user111");
		CreateBagBean.setApplyMemo("自动化测试创建套券领取优惠券申请理由");
		CreateBagBean.setEffectiveType(0);
		CreateBagBean.setEffectiveValidType(0);
		CreateBagBean.setEffectiveDays(0);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		CreateBagBean.setValidStart(dBefore);
		CreateBagBean.setValidEnd(dAfter);
		CreateBagBean.setAcquireStartTime(dBefore);
		CreateBagBean.setAcquireEndTime(dAfter);
		// 获取优惠券方式(1:领取(默认) 2:兑换)
		CreateBagBean.setAcquireMethod(1);
		CreateBagBean.setTotalNum(10);
		CreateBagBean.setReceiveTimesPerUser(1);
		CreateBagBean.setReceiveUserType(0);
		List<Integer> usePlatforms = new ArrayList<Integer>();
		/*
		 * usePlatforms.add(1); usePlatforms.add(2); usePlatforms.add(3);
		 */
		CreateBagBean.setUsePlatformIds(usePlatforms);
		CreateBagBean.setApplyDepartment(1);
		String impresetInvoiceId = EnvSetup.getData("ImpresetInvoiceId");
		CreateBagBean.setImpresetInvoiceId(impresetInvoiceId);

		List<BatchInfo> BatchInfos = new ArrayList<BatchInfo>();
		BatchInfo info = new BatchInfo();
		info.setCouponName("自动化创建套券优惠券名称-子套券");
		info.setCouponDes("自动化创建套券优惠券描述-子套券");
		info.setApplyMemo("自动化创建申请理由-子套券");
		info.setOrderMinAmount(new BigDecimal(2));
		info.setDeductionMaxAmount(new BigDecimal(1));
		List<String> productIds = new ArrayList<String>();
		info.setProductIds(productIds);
		List<Integer> SellerIds = new ArrayList<Integer>();
		info.setSellerIds(SellerIds);
		List<Integer> ActivityIds = new ArrayList<Integer>();
		info.setActivityIds(ActivityIds);
		List<Integer> Brands = new ArrayList<Integer>();
		info.setBrands(Brands);
		List<Integer> Countrys = new ArrayList<Integer>();
		info.setCountrys(Countrys);
		List<Integer> Categories = new ArrayList<Integer>();
		info.setCategories(Categories);
		info.setLimitPspProduct(false);
		BatchInfos.add(info);
		CreateBagBean.setBatchInfos(BatchInfos);
		return CreateBagBean;
	}

	/**
	 * 根据传入bean创建套券优惠券
	 * 
	 * @param couponBatchBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateBagCall createPlatformCouponBag(CreateBagBean createBagBean)
			throws ClientProtocolException, IOException {
		Logger.comment("根据传入createcouponBean创建优惠券");

		CreateBagCall createBagCall = new CreateBagCall();

		createBagCall.setData(false, createBagBean);
		createBagCall.callService();

		return createBagCall;
	}

	/**
	 * 根据传入bean创建平台优惠券
	 * 
	 * @param couponBatchBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateCouponCall createPlatformCoupon(
			CreateCouponBean createcouponBean) throws ClientProtocolException,
			IOException {
		Logger.comment("根据传入createcouponBean创建优惠券");

		CreateCouponCall createcouponCall = new CreateCouponCall();
        if(createcouponBean.getPlatformSource()==1)
        	createcouponBean.setAcquireCouponMethod(1);
		createcouponCall.setData(createcouponBean);
		createcouponCall.callService();

		return createcouponCall;
	}

	/**
	 * 用户领取优惠券
	 * 
	 * 需要提前创建优惠券
	 * 
	 * @param batchCode
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public UserReceiveCouponCall receiveCouponInfo(String batchCode, int userId)
			throws ClientProtocolException, IOException {
		Logger.comment("用户" + userId + "领取优惠券，批次为" + batchCode);
		// new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);

		UserReceiveCouponBean bean = new UserReceiveCouponBean();
		UserReceiveCouponCall call = new UserReceiveCouponCall();

		bean.setBatchCode(batchCode);
		bean.setUserId(userId);

		call.setData(false, bean);
		call.callService();

		return call;
	}

	/**
	 * 用户领取优惠券
	 * 
	 * 需要提前创建优惠券
	 * 
	 * @param batchCode
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public UserReceiveCouponCall receiveCouponInfo(String batchCode,
			int userId, int buyerOrderNum) throws ClientProtocolException,
			IOException {
		Logger.comment("用户" + userId + "领取优惠券，批次为" + batchCode);
		// new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);

		UserReceiveCouponBean bean = new UserReceiveCouponBean();
		UserReceiveCouponCall call = new UserReceiveCouponCall();

		bean.setBatchCode(batchCode);
		bean.setUserId(userId);
		bean.setBuyOrderNum(buyerOrderNum);

		call.setData(false, bean);
		call.callService();

		return call;
	}

	// CreateSendTask
	public static CreateSendTaskBean CreateDefaultSendTaskBeanForPlatform(
			String batchCode) throws ClientProtocolException, IOException {
		Logger.comment("根据传入CouponCode创建平台券默认的CreateSendTaskBean");

		CreateSendTaskBean CreateSendTaskBean = new CreateSendTaskBean();
		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();

		taskSendReqInfo.setBatchCode(batchCode);

		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);

		// 发放用户
		taskSendReqInfo.setSendUsers(list);
		// 商家ID，0：代表平台
		taskSendReqInfo.setSellerId(0);
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(1111);
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		// 商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		CreateSendTaskBean.setTaskSendReqInfo(taskSendReqInfo);

		return CreateSendTaskBean;
	}

	/**
	 * 
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateSendTaskBean CreateDefaultSendTaskBeanForPlatform()
			throws ClientProtocolException, IOException {
		Logger.comment("创建平台券默认的CreateSendTaskBean");
		CreateSendTaskBean CreateSendTaskBean = new CreateSendTaskBean();

		CreateCouponBean createCouponBean = setDefaultPlatformCreateCouponBeanForRecieve();

		CreateCouponCall createCouponCall = createPlatformCoupon(createCouponBean);

		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();

		taskSendReqInfo.setBatchCode(createCouponCall.getBatchCode());

		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);

		// 发放用户
		taskSendReqInfo.setSendUsers(list);
		// 商家ID，0：代表平台
		taskSendReqInfo.setSellerId(0);
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(1111);
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		// 商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		CreateSendTaskBean.setTaskSendReqInfo(taskSendReqInfo);

		return CreateSendTaskBean;
	}

	/**
	 * 
	 * @param createSendTaskBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateSendTaskCall CreateSendTask(
			CreateSendTaskBean createSendTaskBean)
			throws ClientProtocolException, IOException {
		CreateSendTaskCall createsendtaskCall = new CreateSendTaskCall();

		createsendtaskCall.setData(createSendTaskBean);
		createsendtaskCall.callService();

		return createsendtaskCall;
	}

	/**
	 * 
	 * @param batchCode
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static SendCouponBean CreateDefaultSendCouponBean(Integer sellerId,
			String batchCode) throws ClientProtocolException, IOException {
		Logger.comment("根据传入BatchCode创建商家券默认的CreateSendTaskBean");
		SendCouponBean sendcouponBean = new SendCouponBean();

		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();

		taskSendReqInfo.setBatchCode(batchCode);

		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);

		// 发放用户
		taskSendReqInfo.setSendUsers(list);
		// 商家ID，0：代表平台
		taskSendReqInfo.setSellerId(sellerId);
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		if (sellerId == 0) {
			taskSendReqInfo.setOperatorId(12345);
		} else {
			taskSendReqInfo.setOperatorId(sellerId);
		}
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		// 商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		sendcouponBean.setTaskSendReqInfo(taskSendReqInfo);

		return sendcouponBean;
	}

	/**
	 * 
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static SendCouponBean CreateDefaultSendCouponBean()
			throws ClientProtocolException, IOException {
		Logger.comment("创建商家券默认的CreateSendTaskBean");
		SendCouponBean sendcouponBean = new SendCouponBean();

		CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
		SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2
				.setDefaultSellerCreateBatchBean();
		SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2
				.SellerCreateBatch(sellercreatebatchBean);

		TaskSendReqInfo taskSendReqInfo = new TaskSendReqInfo();

		taskSendReqInfo.setBatchCode(sellerCreateBatchCall.getBatchCode());

		int buyerId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		HashSet<Integer> list = new HashSet();
		list.add(buyerId);

		// 发放用户
		taskSendReqInfo.setSendUsers(list);
		// 商家ID，0：代表平台
		taskSendReqInfo.setSellerId(sellercreatebatchBean.getSellerId());
		// 操作者ID，对于商家可能是SellerId，对于平台是当前登录者ID
		taskSendReqInfo.setOperatorId(sellercreatebatchBean.getSellerId());
		// 操作者姓名，对于商家可能是商家姓名，对于平台是当前登录者姓名，不能为NULL
		taskSendReqInfo.setOperatorName("自动化OperatorName");
		// 商家店铺名称，对于平台需要与产品确认，用于显示在通知消息内容里，不能为NULL
		taskSendReqInfo.setSellerShopName("自动化SellerShopName");
		// 业务类型，用于消息通知
		taskSendReqInfo.setBusinessType(1);
		sendcouponBean.setTaskSendReqInfo(taskSendReqInfo);

		return sendcouponBean;
	}

	/**
	 * 
	 * @param sendCouponBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public SendCouponCall SendCoupon(SendCouponBean sendCouponBean)
			throws ClientProtocolException, IOException {
		Logger.comment("发送创建商家券请求");
		SendCouponCall sendcouponCall = new SendCouponCall();
		sendcouponCall.setData(sendCouponBean);
		sendcouponCall.callService();
		return sendcouponCall;
	}

	public SendSmallCouponBean setSendsmallcouponBean(List<Integer> userids,
			String batchcode, Integer sellerId, String sellerShopName) {
		SendSmallCouponBean sendSmallCouponBean = new SendSmallCouponBean();

		sendSmallCouponBean.setUserIds(userids);
		sendSmallCouponBean.setBatchCode(batchcode);
		sendSmallCouponBean.setSellerId(sellerId);
		sendSmallCouponBean.setSellerShopName(sellerShopName);

		return sendSmallCouponBean;
	}

	/**
	 * 
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public SendSmallCouponBean setSendSmallCouponBean()
			throws ClientProtocolException, IOException {
		Logger.comment("创建商家券默认的SendSmallCouponBean");
		SendSmallCouponBean sendSmallCouponBean = new SendSmallCouponBean();

		List<Integer> userIds = new ArrayList<>();

		userIds.add(TestDataManager.getRandomIntNum(8));
		userIds.add(TestDataManager.getRandomIntNum(8));
		userIds.add(TestDataManager.getRandomIntNum(8));

		// 创建优惠券批次
		CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
		SellerCreateBatchBean sellercreatebatchBean = CouponServiceMCallServiceV2
				.setDefaultSellerCreateBatchBean();
		SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2
				.SellerCreateBatch(sellercreatebatchBean);
        //分销
		couponbatchWapper couponbatchWapper=new couponbatchWapper();
		couponbatchWapper.setPlatformSource(1, sellerCreateBatchCall.getBatchCode());
		
		return setSendsmallcouponBean(userIds,
				sellerCreateBatchCall.getBatchCode(),
				sellercreatebatchBean.getSellerId(), "商家店铺名称sss");
	}
	/**
	 * 
	 * @param batchCode
	 * @return
	 */
	public UserReceiveCouponBean setUserReceiveCouponBean(String batchCode) {
		UserReceiveCouponBean userreceivecouponBean = new UserReceiveCouponBean();

		userreceivecouponBean.setUserId(Integer.valueOf(EnvSetup
				.getData("BuyUserId")));
		userreceivecouponBean.setBatchCode(batchCode);
		userreceivecouponBean.setBuyOrderNum(10);
		userreceivecouponBean.setBusinessType(1);
		userreceivecouponBean.setSellerShopName("商家店铺名称");

		return userreceivecouponBean;
	}

	/**
	 * 
	 * @param batchCodes
	 * @return
	 */
	public UserReceivePackageBean setUserReceivePackageBean(
			List<String> batchCodes) {
		UserReceivePackageBean userReceivePackageBean = new UserReceivePackageBean();

		userReceivePackageBean.setUserId(Integer.valueOf(EnvSetup
				.getData("BuyUserId")));
		userReceivePackageBean.setBatchCodes(batchCodes);
		userReceivePackageBean.setBuyOrderNum(10);
		userReceivePackageBean.setBusinessType(1);
		userReceivePackageBean.setSellerShopName("商家店铺名称");

		return userReceivePackageBean;
	}

	/**
	 * 设置默认的领取套券bean
	 * @param couponBagCode
	 * @return
	 */
	public userReceiveCouponBagBean setuserReceiveCouponBagBean(
			String couponBagCode) {
		userReceiveCouponBagBean userReceiveCouponBagBean = new userReceiveCouponBagBean();

		userReceiveCouponBagBean.setUserId(Integer.valueOf(EnvSetup
				.getData("BuyUserId")));
		userReceiveCouponBagBean.setUserType(0);
		userReceiveCouponBagBean.setCouponBagCode(couponBagCode);
		return userReceiveCouponBagBean;
	}
	public userReceiveCouponBagCall userReceiveCouponBagCall(
			userReceiveCouponBagBean userReceiveCouponBagBean)
			throws ClientProtocolException, IOException {

		userReceiveCouponBagCall userReceiveCouponBagCall = new userReceiveCouponBagCall();
		userReceiveCouponBagCall.setData(userReceiveCouponBagBean);
		userReceiveCouponBagCall.callService();
		return userReceiveCouponBagCall;
	}

	/**
	 * 设置默认的创建商家优惠券券bean
	 * 
	 * @return
	 */
	public SellerCreateBatchBean setDefaultSellerCreateBatchBean() {
		SellerCreateBatchBean sellerCreateBatchBean = new SellerCreateBatchBean();

		int sellerid = TestDataManager.getRandomIntNum(6);
		sellerCreateBatchBean.setSellerId(sellerid);
		sellerCreateBatchBean.setBatchName("java化商家优惠券批次Name");
		sellerCreateBatchBean.setBatchDes("创建商家优惠券批次描述");

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 
		dAfter = calendar2.getTime(); // 得到前一天的时间

		sellerCreateBatchBean.setEffectiveStartDate(dBefore);
		sellerCreateBatchBean.setEffectiveEndDate(dAfter);

		sellerCreateBatchBean.setCouponAmount(new BigDecimal(1));
		sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(2));

		sellerCreateBatchBean.setCouponTotalCount(1000);
		sellerCreateBatchBean.setReceiveNumPerUser(1);
		sellerCreateBatchBean.setIsShowOnPage(true);
		sellerCreateBatchBean.setCouponType((byte) 1);

		return sellerCreateBatchBean;
	}

	
	
	/**
	 * 设置默认的创建商家优惠券券bean，用于活动优惠券
	 * 
	 * @return
	 */
	public SellerCreateBatchBean setDefaultSellerCreateBatch_ActivityBean() {
		SellerCreateBatchBean sellerCreateBatchBean = new SellerCreateBatchBean();

		int sellerid = TestDataManager.getRandomIntNum(6);
		sellerCreateBatchBean.setSellerId(sellerid);
		sellerCreateBatchBean.setBatchName("java化商家平台活动优惠券批次Name");
		sellerCreateBatchBean.setBatchDes("创建商家平台活动优惠券批次描述");

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 
		dAfter = calendar2.getTime(); // 得到前一天的时间

		sellerCreateBatchBean.setEffectiveStartDate(dBefore);
		sellerCreateBatchBean.setEffectiveEndDate(dAfter);
		//新增
		sellerCreateBatchBean.setAcquireStartTime(dBefore);
		sellerCreateBatchBean.setAcquireEndTime(dAfter);

		sellerCreateBatchBean.setCouponAmount(new BigDecimal(1));
		sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(2));

		sellerCreateBatchBean.setCouponTotalCount(1000);
		sellerCreateBatchBean.setReceiveNumPerUser(1);
		sellerCreateBatchBean.setIsShowOnPage(true);
		sellerCreateBatchBean.setCouponType((byte) 3);
		//新增
		sellerCreateBatchBean.setCouponTypeBizValue(Integer.toString(new TestDataManager().getRandomIntNum(5)));

		return sellerCreateBatchBean;
	}
	/**
	 *  创建商家优惠券批次
	 *  
	 * @param sellerCreateBatchBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws  
	 */
	public SellerCreateBatchCall SellerCreateBatch(
			SellerCreateBatchBean sellerCreateBatchBean)
			throws ClientProtocolException, IOException {
		Logger.comment("创建商家优惠券批次");

//		deleteSellerBatchCountInCurrentMonthBysellerIdAnd30(sellerCreateBatchBean);
		SellerCreateBatchCall sellerCreateBatchCall = new SellerCreateBatchCall();
		sellerCreateBatchCall.setData(false, sellerCreateBatchBean);
		sellerCreateBatchCall.callService();

		return sellerCreateBatchCall;
	}

	/**
	 *  作废商家优惠券批次
	 * @param sellerId
	 * @param batchId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public InvalidBatchCall InvalidSellerBatch(Integer sellerId,
			String batchCode) throws ClientProtocolException, IOException {
		Logger.comment("作废商家优惠券批次");

		InvalidBatchBean invalidBatchBean = new InvalidBatchBean();
		InvalidBatchCall invalidBatchCall = new InvalidBatchCall();

		invalidBatchBean.setSellerId(sellerId);
		invalidBatchBean.setBatchCode(batchCode);

		invalidBatchCall.setData(false, invalidBatchBean);
		invalidBatchCall.callService();

		return invalidBatchCall;
	}

	/**
	 *  作废平台优惠券批次
	 * @param batchId
	 * @param invalidUser
	 * @param invalidMemo
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public InvalidBatchCall InvalidPlatformBatch(String batchCode,
			String invalidUser, String invalidMemo)
			throws ClientProtocolException, IOException {
		Logger.comment("作废平台优惠券批次");

		InvalidBatchBean invalidBatchBean = new InvalidBatchBean();
		InvalidBatchCall invalidBatchCall = new InvalidBatchCall();

		invalidBatchBean.setSellerId(0);
		invalidBatchBean.setInvalidUser(invalidUser);
		invalidBatchBean.setInvalidMemo(invalidMemo);
		invalidBatchBean.setBatchCode(batchCode);

		invalidBatchCall.setData(false, invalidBatchBean);
		invalidBatchCall.callService();

		return invalidBatchCall;
	}

	public static ShoppingInfo setDefaultShoppingInfo(int buyerId,
			String platformCouponCode, String sellerCouponCode) {
		Logger.comment("设置默认的购物车");
		ShoppingInfo shoppingInfo = new ShoppingInfo();

		BuyerInfo buyerInfo = new BuyerInfo();

		buyerInfo.setBuyerId(buyerId);
		// 默认下单数0
		buyerInfo.setBuyerOrderNum(0);

		shoppingInfo.setBuyerInfo(buyerInfo);

		SellerOrderInfo sellerOrderInfo = new SellerOrderInfo();

		// 卖家Id
		int sellerId = Integer.parseInt(EnvSetup.getData("jCouponSellerId"));
		sellerOrderInfo.setSellerId(sellerId);

		if (sellerCouponCode != null && sellerCouponCode != "") {
			sellerOrderInfo.setCouponCode(sellerCouponCode);
		}

		OrderInfo orderInfo = new OrderInfo();
		ProductInfo productInfo = new ProductInfo();

		String productCode = TestDataManager.getRandomNum(36);
		productInfo.setProductCode(productCode);
		productInfo.setActivityId(0);
		productInfo.setIsNotAllowUsePlatformCoupon(false);
		productInfo.setIsNotAllowUseSellerCoupon(false);
		
		productInfo.setBrandId(2111);
		productInfo.setCategoryId(3111);
		productInfo.setProductQuantity(1);
		productInfo.setProductPrice(new BigDecimal(1000));

		List<ProductInfo> productInfos = new ArrayList<ProductInfo>();
		productInfos.add(productInfo);
		orderInfo.setProductInfos(productInfos);

		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		orderInfos.add(orderInfo);

		sellerOrderInfo.setOrderInfos(orderInfos);

		List<SellerOrderInfo> sellerOrderInfos = new ArrayList<SellerOrderInfo>();
		sellerOrderInfos.add(sellerOrderInfo);

		shoppingInfo.setSellerOrderInfos(sellerOrderInfos);

		if (platformCouponCode != null && platformCouponCode != "") {
			shoppingInfo.setPlatformCouponCode(platformCouponCode);
		}

		shoppingInfo.setPlatformUsage(1);
		shoppingInfo.setCanUseGroupCoupon(false);

		return shoppingInfo;
	}

	public static ShoppingInfo setDefaultShoppingInfo(int buyerId,
			Integer sellerId, String platformCouponCode, String sellerCouponCode) {
		Logger.comment("设置默认的购物车");
		ShoppingInfo shoppingInfo = setDefaultShoppingInfo(buyerId,
				platformCouponCode, sellerCouponCode);
		shoppingInfo.getSellerOrderInfos().get(0).setSellerId(sellerId);
		return shoppingInfo;
	}

	public UpdateApplyProductsBean setUpdateApplyProductsBean(int batchId,
			List<String> productCodes) {
		UpdateApplyProductsBean updateapplyproductsBean = new UpdateApplyProductsBean();

		updateapplyproductsBean.setBatchId(batchId);
		updateapplyproductsBean.setProductCodes(productCodes);

		return updateapplyproductsBean;
	}

	public UpdateApplyProductsCall UpdateApplyProducts(
			UpdateApplyProductsBean updateApplyProductsBean)
			throws ClientProtocolException, IOException {
		UpdateApplyProductsCall call = new UpdateApplyProductsCall();
		call.setData(false, updateApplyProductsBean);
		call.callService();

		return call;
	}

	public createTaskByUserIdBean setCreateTaskByUserIdBeanForPlatform()
			throws ClientProtocolException, IOException {
		createTaskByUserIdBean createTaskByUserIdBean = new createTaskByUserIdBean();
		CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();
		CreateCouponBean createCouponBean = couponCallService
				.setDefaultPlatformCreateCouponBeanForRecieve();
		createCouponBean.setCouponNum(1000);
		CreateCouponCall createCouponCall = couponCallService
				.createPlatformCoupon(createCouponBean);

		List<Integer> userIds = new ArrayList<Integer>();
		userIds.add(11);
		createTaskByUserIdBean.setUserIds(userIds);
		createTaskByUserIdBean.setBatchType(1);
		createTaskByUserIdBean.setBatchCode(createCouponCall.getBatchCode());
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);

		return createTaskByUserIdBean;
	}
	//普通优惠券-创建错误任务
	public createTaskByUserIdBean setCreateTaskByUserIdBeanForFailPlatform()
			throws ClientProtocolException, IOException {

		int userId = Integer.parseInt(TestDataManager.getRandomNum(5));
		createTaskByUserIdBean createTaskByUserIdBean = new createTaskByUserIdBean();
		CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();
		CreateCouponBean createCouponBean = couponCallService
				.setDefaultPlatformCreateCouponBeanForRecieve();

		CreateCouponCall createCouponCall = couponCallService
				.createPlatformCoupon(createCouponBean);

		couponCallService.receiveCouponInfo(createCouponCall.getBatchCode(),
				userId);

		couponCallService.receiveCouponInfo(createCouponCall.getBatchCode(),
				userId);

		List<Integer> userIds = new ArrayList<Integer>();
		userIds.add(111);

		createTaskByUserIdBean.setUserIds(userIds);
		createTaskByUserIdBean.setBatchType(1);
		createTaskByUserIdBean.setBatchCode(createCouponCall.getBatchCode());
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);

		return createTaskByUserIdBean;
	}

	public createTaskByUserIdCall couponsendsubtaskFail(
			createTaskByUserIdBean bean) throws ClientProtocolException,
			IOException {
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		call.setData(bean);
		call.callService();
		call.callService();
		return call;
	}
	public createTaskByUserIdCall createTaskByUserId(createTaskByUserIdBean bean)
			throws ClientProtocolException, IOException {
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		call.setData(bean);
		call.callService();

		return call;
	}
	/**
	 * 给用户发优惠券
	 * @param userid
	 * @param batchcode
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public createTaskByUserIdCall createTaskByUserId(Integer userid,String batchcode)
			throws ClientProtocolException, IOException {
		createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
		List<Integer> userIds = new ArrayList<Integer>();
		userIds.add(userid);
		createTaskByUserIdBean.setUserIds(userIds);
//		createTaskByUserIdBean.setBatchType(1);
		createTaskByUserIdBean.setBatchCode(batchcode);
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		call.setData(createTaskByUserIdBean);
		call.callService();
		return call;
	}
	/*
	 * JOB回调更新批次已领取和使用数量
	 */
	public void updatereceivedandusedcount() throws ClientProtocolException,
			IOException {
		Logger.comment("更新批次已领取和使用数量,用JOB回调,为了测试方便，目前直接调");
		updateReceivedAndUsedCountBean updatereceivedandusedcountBean = new updateReceivedAndUsedCountBean();
		updateReceivedAndUsedCountCall updatereceivedandusedcountCall = new updateReceivedAndUsedCountCall();
		//更新couponupdatesetting为当前时间
		couponupdatesetting couponupdatesetting = new couponupdatesettingWapper()
				.selectTop1();
		couponupdatesetting.setLastUpdateTime(new Date());
		new couponupdatesettingWapper().updateByPrimaryKey(couponupdatesetting);
		updatereceivedandusedcountCall.setData(updatereceivedandusedcountBean);
		updatereceivedandusedcountCall.callService();
	}
	public UploadCall fileUpload(String fileName)
			throws ClientProtocolException, IOException {
		String turl = "";
		String fliename = System.getProperty("user.dir") + File.separator
				+ "resource" + File.separator + "Coupon/" + fileName + "";
		Logger.comment("添加上传文件:" + fliename);
		UploadCall uploadcall = new UploadCall();
		uploadcall.addPart("file", fliename);
		uploadcall.callService();
		return uploadcall;
	}
	//用于压测准备batch数据
	public void StressBatchData() throws ClientProtocolException, IOException {
		//用于压测准备数据
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.HOUR, 5); // 设置为后一分//MINUTE，SECOND秒
		dBefore = calendar1.getTime(); // 设置为后一分

		while (dNow.getTime() < dBefore.getTime()) {
			dNow = new Date(); // 当前时间
			int rand = TestDataManager.uniform(9);
			int randType = TestDataManager.uniform(9);
			if (randType % 2 == 0) {
				// 平台券
				CouponServiceMCallServiceV2 couponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
				CreateCouponBean createCouponBean = couponServiceMCallServiceV2
						.setDefaultPlatformCreateCouponBeanForRecieve();
				if (rand % 2 == 0) {
					createCouponBean.setSendType(2);
					createCouponBean.setCouponUseType(2);
					createCouponBean.setUserType(1);
					createCouponBean.setAcquireCouponMethod(2);
				} else if (rand % 3 == 0) {
					createCouponBean.setUserType(2);
				}
				CreateCouponCall createCouponCall = couponServiceMCallServiceV2
						.createPlatformCoupon(createCouponBean);
			} else {
				//商家
				CouponServiceMCallServiceV2 couponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
				SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallServiceV2
						.setDefaultSellerCreateBatchBean();
				if (rand % 2 == 0) {
					sellerCreateBatchBean.setCouponType((byte) 1);
				}
				couponServiceMCallServiceV2
						.SellerCreateBatch(sellerCreateBatchBean);
			}
		}
		System.out.println("OK");
	}

	/**
	 * 领用YMT平台优惠券默认链接优惠券
	 * @param userId
	 * @param orderAmount
	 * @param couponValue
	 * @return
	 */
	public static String receiveYMTCoupon(Long userId, BigDecimal orderAmount,
			BigDecimal couponValue) {

		Logger.comment(String.format("用户 %s领用YMT平台优惠券,满 %s 减 %s", userId,
				orderAmount, couponValue));

		CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();

		CreateCouponBean createCouponBean = couponCallService
				.setDefaultPlatformCreateCouponBeanForRecieve();

		//createCouponBean.setImpresetInvoiceId("FBY2018030910141324");

		//订单满多少
		createCouponBean.getCouponValues().get(0)
				.setOrderMinAmount(orderAmount);

		//优惠券减多少
		createCouponBean.getCouponValues().get(0)
				.setDeductionMaxAmount(couponValue);

		CreateCouponCall createCouponCall;

		String batchCode = null;
		try {
			createCouponCall = couponCallService
					.createPlatformCoupon(createCouponBean);
		} catch (Exception e) {

			Logger.info("###############创建YMT平台优惠券批次失败");

			return null;
		}

		try {

			batchCode = createCouponCall.getBatchCode();

			return couponCallService
					.receiveCouponInfo(batchCode, userId.intValue())
					.getUserReceiveResult().getCouponCode();

		} catch (Exception e) {

			Logger.info(String.format(
					"###############领取YMT平台，优惠券批次 batchCode %s,优惠券失败失败",
					batchCode));

			return null;
		}
	}

	/**
	 * 领用商家优惠券
	 * @param userId
	 * @param sellerId
	 * @param orderAmount	订单金额
	 * @param sellerCouponValue	优惠券减免金额
	 * @return
	 */
	public static String receiveSellerCoupon(Long userId, Long sellerId,
			BigDecimal orderAmount, BigDecimal sellerCouponValue) {

		Logger.comment(String.format("用户 %s领用商家 sellerid %s 优惠优惠券,满 %s 减 %s",
				userId, sellerId, orderAmount, sellerCouponValue));

		String batchCode = CouponCallServiceV2
				.callGetSellerReceiveBatchesCall(sellerId.intValue());

		if (StringUtils.isEmpty(batchCode)) {

			// 创建优惠券
			CouponServiceMCallServiceV2 couponServiceMCallService = new CouponServiceMCallServiceV2();

			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService
					.setDefaultSellerCreateBatchBean();

			sellerCreateBatchBean.setCouponTotalCount(30000);
			sellerCreateBatchBean.setOrderMinAmount(orderAmount);
			sellerCreateBatchBean.setSellerId(sellerId.intValue());
			sellerCreateBatchBean.setCouponAmount(sellerCouponValue);
			sellerCreateBatchBean.setReceiveNumPerUser(30000);

			SellerCreateBatchCall sellerCreateBatchCall;

			try {

				sellerCreateBatchCall = couponServiceMCallService
						.SellerCreateBatch(sellerCreateBatchBean);

				batchCode = sellerCreateBatchCall.getBatchCode();

			} catch (Exception e) {

				Logger.info(String.format(
						"###############创建商家 sellerId %s，优惠券批次失败", sellerId));
				return null;
			}

		}

		try {

			return new CouponServiceMCallServiceV2()
					.receiveCouponInfo(batchCode, userId.intValue())
					.getUserReceiveResult().getCouponCode();

		} catch (Exception e) {

			Logger.info(String
					.format("###############领取商家 sellerId %s，优惠券批次 batchCode %s,优惠券失败失败",
							sellerId, batchCode));
			return null;
		}

	}

	public UseCouponCall UseCoupon(Integer buyUserId,String CouponCode) throws ClientProtocolException, IOException
	{
		int orderId = TestDataManager.getRandomIntNum(10);
		List<String> CouponCodes=new ArrayList();
		CouponCodes.add(CouponCode);
		
		UseCouponBean usecouponBean=new UseCouponBean();
		UseCouponCall useCouponCall=new UseCouponCall();
		usecouponBean.setUserId(buyUserId);
		usecouponBean.setMainOrderId(orderId);
		usecouponBean.setCouponCodes(CouponCodes);
		usecouponBean.setAppId("mansit");
		usecouponBean.setRequestId("12344566");
		useCouponCall.setData(usecouponBean);
		useCouponCall.callService();
		return useCouponCall;
	}
	/**
	 * 当月买手优惠券大于等于30条，就删除买手优惠券批次和批次对应的优惠券
	 * @param sellerCreateBatchBean
	 * @return
	 */
	public boolean deleteSellerBatchCountInCurrentMonthBysellerIdAnd30( SellerCreateBatchBean sellerCreateBatchBean)
	{
		try {
	        Date date=new Date();
	        Calendar cale = Calendar.getInstance();
			String dateStart=cale.get(Calendar.YEAR)+"-"+(cale.get(Calendar.MONTH) + 1)+"-01 00:00:01";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date startTime;
			startTime = sdf.parse(dateStart);
			couponbatchWapper couponBatchWapper=new  couponbatchWapper();
			couponWapper couponWapper=new  couponWapper();
			List<couponbatch> list =couponBatchWapper.getSellerBatchCountInCurrentMonth(sellerCreateBatchBean.getSellerId(), startTime);
			if(list.size()>=30)
			{
				for(couponbatch batch:list)
				{
					couponWapper.deleteBybatchId(batch.getBatchId());//删优惠券
				}
				couponBatchWapper.deleteSellerBatchCountInCurrentMonth(sellerCreateBatchBean.getSellerId(), startTime);//删优惠券批次
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static void main(String[] args) {
		try {
			new CouponServiceMCallServiceV2().StressBatchData();
			//			createCouponBean.setCouponNum(200000000);
			//			createCouponBean.setPerUserReceiveMaxCount(1);
			//			//createCouponBean.setPerCouponMaxUseCount(2000);
			//			int i=0;
			//			while(i<3)
			//			{
			//			CreateCouponCall createCouponCall= CouponServiceMCallServiceV2.createPlatformCoupon(createCouponBean);
			//			 System.out.println(createCouponCall.getBatchCode());
			//
			//			i++;
			//			}

			// 商家券
			//			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	
			//
			//			String [] userStrings = {"21158951","21158981","21159000","21159010","21159016","21159026","21159040","21159043","21159050","21159080","21159091","21159097","21159108","21159120","21159126","21159133","21159145","21159153","21159168"};
			//			// 商家优惠券
			//			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			//			
			//			couponBatchBean.setCouponTotalCount(200000000);
			//			couponBatchBean.setReceiveNumPerUser(2000);
			//			
			//			for (String string : userStrings) {
			//				couponBatchBean.setSellerId(Integer.parseInt(string));
			//
			//				for(int i=0;i<10;i++)
			//				{
			//				System.out.println(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			//				}
			//			}

			//			 System.out.println(createSellerCouponCall.getBatchCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}
